package com.vinci.task.service.impl;

import com.vinci.task.entity.TbTaskInfo;
import com.vinci.task.entity.TbTaskRewards;
import com.vinci.task.entity.TbTaskRewardsRecord;
import com.vinci.task.mapper.TbTaskInfoMapper;
import com.vinci.task.mapper.TbTaskRewardsMapper;
import com.vinci.task.mapper.TbTaskRewardsRecordMapper;
import com.vinci.task.service.TbTaskRewardsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vinci.util.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 奖励积分表 服务实现类
 * </p>
 *
 * @author 张先生
 * @since 2021-12-11
 */
@Service
public class TbTaskRewardsServiceImpl extends ServiceImpl<TbTaskRewardsMapper, TbTaskRewards> implements TbTaskRewardsService {

    @Autowired(required = false)
    private TbTaskRewardsMapper tbTaskRewardsMapper;
    @Autowired(required = false)
    private TbTaskRewardsRecordMapper tbTaskRewardsRecordMapper;
    @Autowired(required = false)
    private TbTaskInfoMapper tbTaskInfoMapper;



    /**
     * 审核任务是否完成
     * @param tbTaskRewards
     * @param session
     */

    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    @Override
    public JsonObject insertRewards(TbTaskRewards tbTaskRewards, HttpSession session) {

        JsonObject jsonObject = new JsonObject();

        try {
            TbTaskInfo task = new TbTaskInfo();
            String taskId = tbTaskRewards.getTaskId();
            Integer whether = tbTaskRewards.getWhether();
            Integer rewardsType = tbTaskRewards.getRewardsType();



            String goldCoinsSum3 = tbTaskRewards.getGoldCoins();
            String integralSum3 = tbTaskRewards.getIntegral();


            String userId = tbTaskRewards.getUserId();
            String createBy = tbTaskRewards.getCreateBy();


            String[] strSplit = goldCoinsSum3.split(",");
            String[] strSplit2 = integralSum3.split(",");
            String[] strSplit3 = userId.split(",");

            Integer integralSum2=0;
            Integer goldCoinsSum2=0;
            for(int i=0;i<strSplit.length;i++){
                String coin =strSplit[i];
                int c1 = Integer.parseInt(coin);
                goldCoinsSum2+=c1;

                String integral =strSplit2[i];
                int i1 = Integer.parseInt(integral);
                integralSum2+=i1;
            }


            task.setTaskId(taskId);
            List<TbTaskInfo> tbTaskInfos = tbTaskInfoMapper.selectTbTaskInfo(task);
            for(TbTaskInfo zzk:tbTaskInfos){

                Integer integral1 = zzk.getIntegral();
                Integer goldCoins = zzk.getGoldCoins();


                if(integralSum2 <= integral1 && goldCoinsSum2 <= goldCoins){

                    TbTaskRewards tbTaskRewards2 = tbTaskRewardsMapper.selectRewardsCreateBy(rewardsType,createBy);
                    Integer integralSum1 = tbTaskRewards2.getIntegralSum();
                    Integer goldCoinsSum1 = tbTaskRewards2.getGoldCoinsSum();
                    String rewardsId = tbTaskRewards2.getRewardsId();

                    if(integralSum1 >= integralSum2 && goldCoinsSum1 >= goldCoinsSum2){
                        String uuid = UUID.randomUUID().toString().replace("-", "");

                        TbTaskRewards tbTaskRewards1 = tbTaskRewardsMapper.selectRewards(rewardsType,userId);



                        if(whether==1){
                            for(int i=0;i<strSplit.length;i++){
                                String gold3 =strSplit[i];
                                String int2 =strSplit2[i];
                                String userId3 =strSplit3[i];

                                int g1 = Integer.parseInt(gold3);

                                int i1 = Integer.parseInt(int2);

                                Integer sun = integralSum1 - i1;
                                Integer gold = goldCoinsSum1 - g1;

                                tbTaskRewards.setGoldCoinsSum(sun);
                                tbTaskRewards.setIntegralSum(gold);
                                tbTaskRewards.setRewardsId(rewardsId);
                                tbTaskRewards.setUpdateTime(new Timestamp(System.currentTimeMillis()));
                                tbTaskRewardsMapper.updateRewards(tbTaskRewards);

                                if(tbTaskRewards1 == null){

                                    tbTaskRewards.setRewardsId(uuid);
                                    tbTaskRewards.setDelflag(0);
                                    tbTaskRewards.setCreateBy(userId3);
                                    tbTaskRewards.setUserId(userId3);
                                    tbTaskRewards.setGoldCoinsSum(g1);
                                    tbTaskRewards.setIntegralSum(i1);
                                    tbTaskRewards.setCreateTime(new Timestamp(System.currentTimeMillis()));

                                    tbTaskRewardsMapper.insertRewards(tbTaskRewards);
                                }else {
                                    Integer goldCoinsSum = tbTaskRewards1.getGoldCoinsSum();
                                    Integer integralSum = tbTaskRewards1.getIntegralSum();
                                    String rewardsId1 = tbTaskRewards1.getRewardsId();

                                    Integer CoinsSum = goldCoinsSum + g1;
                                    Integer integral = integralSum + i1;

                                    tbTaskRewards.setGoldCoinsSum(CoinsSum);
                                    tbTaskRewards.setIntegralSum(integral);
                                    tbTaskRewards.setRewardsId(rewardsId1);

                                    tbTaskRewardsMapper.updateRewards(tbTaskRewards);
                                }
                                TbTaskRewardsRecord tbTaskRewardsRecord = new TbTaskRewardsRecord();
                                tbTaskRewardsRecord.setRecordId(uuid);
                                tbTaskRewardsRecord.setUserId(userId3);
                                tbTaskRewardsRecord.setTaskId(taskId);
                                tbTaskRewardsRecord.setGoldCoins(g1);
                                tbTaskRewardsRecord.setIntegral(i1);
                                tbTaskRewardsRecord.setDelflag(0);
                                tbTaskRewardsRecord.setCreateBy(userId3);
                                tbTaskRewardsRecord.setCreateTime(new Timestamp(System.currentTimeMillis()));
                                tbTaskRewardsRecord.setUpdateTime(new Timestamp(System.currentTimeMillis()));

                                tbTaskRewardsRecordMapper.insertRewardsRecord(tbTaskRewardsRecord);

                                task.setStatus(5);
                                tbTaskInfoMapper.updateClaim(task);
                            }

                        }else {

                            task.setStatus(3);
                            tbTaskInfoMapper.updateClaim(task);
                        }

                    }else {
                        jsonObject.setCode("4");
                        jsonObject.setMsg("金币或余额不足");
                    }

                }else {
                    jsonObject.setCode("3");
                    jsonObject.setMsg("超出任务金币范围");
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.setCode("5");
            jsonObject.setMsg("数据异常");
        }

        return jsonObject;
    }
}
